package com.government.system.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.government.common.core.domain.entity.SysUser;
import com.government.common.core.domain.model.LoginUser;
import com.government.member.domain.MemberInfo;
import com.government.member.domain.Vip;
import com.government.member.mapper.MemberInfoMapper;
import com.government.member.mapper.VipMapper;
import com.government.system.domain.WebRoleAuth;
import com.government.system.domain.vo.WebAuthVo;
import com.government.system.mapper.WebRoleAuthMapper;
import com.government.system.service.WebRoleAuthService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

import static com.government.common.utils.SecurityUtils.getLoginUser;
import static com.government.common.utils.SecurityUtils.getUserId;

@Service
public class WebRoleAuthServiceImpl implements WebRoleAuthService {
    @Autowired
    private WebRoleAuthMapper webRoleAuthMapper;

    @Autowired
    private VipMapper vipMapper;

    @Autowired
    private MemberInfoMapper memberInfoMapper;

    @Override
    public String getAuthContent(String code) {
        WebRoleAuth webRoleAuth = webRoleAuthMapper.selectOne(Wrappers.<WebRoleAuth>lambdaQuery()
                .eq(WebRoleAuth::getCode, code));
        return webRoleAuth.getContent();
    }

    @Override
    public WebAuthVo getAuthContent() {

        LoginUser loginUser = getLoginUser();
        WebAuthVo webAuthVo = new WebAuthVo();
        List<WebRoleAuth> webRoleAuthList = webRoleAuthMapper.selectList(Wrappers.lambdaQuery());
        Map<String, String> authMap = webRoleAuthList.stream().collect(Collectors.toMap(WebRoleAuth::getCode, WebRoleAuth::getContent));

        SysUser sysUser = null;

        if (Objects.nonNull(loginUser)) {
            sysUser = getVipInfo(loginUser);
            if (!"-VISITOR".equals(sysUser.getVipTypeCode())) {
                // 判断当前用户是否可修改绑定地区
                MemberInfo memberInfo = memberInfoMapper.selectById(sysUser.getUserId());
                Date updAreaTime = null;
                if (Objects.nonNull(memberInfo)) {
                    updAreaTime = memberInfo.getUpdAreaTime();
                    String updAreaMonth = authMap.get("ISC" + sysUser.getVipTypeCode());
                    int month = Integer.parseInt(updAreaMonth) * 30;
                    if (Objects.isNull(updAreaTime) || DateUtil.between(new Date(), updAreaTime, DateUnit.DAY, true) > month) {
                        webAuthVo.setUpdateArea(true);
                    }
                } else {
                    webAuthVo.setUpdateArea(true);
                }
            }
        }
        webAuthVo.setUaTime(authMap.get("UA" + sysUser.getVipTypeCode()));
        webAuthVo.setIscCount(authMap.get("ISC" + sysUser.getVipTypeCode()));

        webAuthVo.setPolicyCount(authMap.get("PSC" + sysUser.getVipTypeCode()));
        webAuthVo.setOrgCount(authMap.get("OSC" + sysUser.getVipTypeCode()));
        webAuthVo.setItemCount(authMap.get("IMSC" + sysUser.getVipTypeCode()));
        webAuthVo.setIsm(authMap.get("ISM" + sysUser.getVipTypeCode()));
        webAuthVo.setPc(authMap.get("PC" + sysUser.getVipTypeCode()));
        webAuthVo.setRpc(authMap.get("RPC" + sysUser.getVipTypeCode()));
        webAuthVo.setCc(authMap.get("CC" + sysUser.getVipTypeCode()));
        webAuthVo.setFc(authMap.get("FC" + sysUser.getVipTypeCode()));
        webAuthVo.setBc(authMap.get("BC" + sysUser.getVipTypeCode()));
        if (!"-VISITOR".equals(sysUser.getVipTypeCode()) && sysUser.getUserId() == 150L) {
            webAuthVo.setIscCount("-1");
            webAuthVo.setOrgCount("-1");
            webAuthVo.setPolicyCount("-1");
            webAuthVo.setItemCount("-1");
            webAuthVo.setUpdateArea(true);
        }
        return webAuthVo;
    }

    @Override
    public SysUser getVipInfo(LoginUser loginUser) {
        SysUser sysUser = new SysUser();
        if (loginUser.isVisitor()) {
            sysUser.setVipTypeCode("-VISITOR");
        } else {
            sysUser = loginUser.getUser();
            List<Vip> vipList = vipMapper.selectList(Wrappers.<Vip>lambdaQuery()
                    .eq(Vip::getUid, sysUser.getUserId())
                    .eq(Vip::getStatus, "1"));
            if (CollUtil.isNotEmpty(vipList)) {
                Map<Integer,Vip> vipMap = vipList.stream().collect(Collectors.toMap(Vip::getVipType,x->x));
                if(vipMap.containsKey(3)) {
                    sysUser.setVip(3);
                    sysUser.setVipType(5);
                    sysUser.setVipExpirationTime(DateUtil.formatDateTime(vipMap.get(3).getMaturityTime()));
                    sysUser.setVipTypeCode("-CVIP");
                } else if(vipMap.containsKey(2)){
                    sysUser.setVip(2);
                    sysUser.setVipType(4);
                    sysUser.setVipExpirationTime(DateUtil.formatDateTime(vipMap.get(2).getMaturityTime()));
                    sysUser.setVipTypeCode("-SVIP");
                } else if(vipMap.containsKey(1)){
                    sysUser.setVip(1);
                    sysUser.setVipType(3);
                    sysUser.setVipExpirationTime(DateUtil.formatDateTime(vipMap.get(1).getMaturityTime()));
                    sysUser.setVipTypeCode("-VIP");
                }
            } else {
                sysUser.setVipType(2);
                sysUser.setVipTypeCode("-USER");
            }
        }

        return sysUser;
    }

}
